package com.sitia.jd.service.impl;

import java.util.ArrayList;
import java.util.List;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sitia.common.CustomException;
import com.sitia.jd.domain.dto.CommonDto;
import com.sitia.jd.domain.dto.productDto;
import com.sitia.jd.domain.vo.MxProductsThirdVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.sitia.jd.mapper.MxProductsThirdMapper;
import com.sitia.jd.domain.MxProductsThird;
import com.sitia.jd.service.IMxProductsThirdService;

/**
 * 第三方商品Service业务层处理
 * 
 * @author sitia
 * @date 2024-06-30
 */
@Service
@Slf4j
public class MxProductsThirdServiceImpl  extends ServiceImpl<MxProductsThirdMapper,MxProductsThird> implements IMxProductsThirdService
{
    @Autowired
    private MxProductsThirdMapper mxProductsThirdMapper;

    /**
     * 查询第三方商品
     * 
     * @param id 第三方商品主键
     * @return 第三方商品
     */
    @Override
    public MxProductsThird selectMxProductsThirdById(String id)
    {
        return mxProductsThirdMapper.selectMxProductsThirdById(id);
    }

    /**
     * 查询第三方商品列表
     * 
     * @param mxProductsThird 第三方商品
     * @return 第三方商品
     */
    @Override
    public List<MxProductsThird> selectMxProductsThirdList(MxProductsThird mxProductsThird)
    {
        return mxProductsThirdMapper.selectMxProductsThirdList(mxProductsThird);
    }

    /**
     * 新增第三方商品
     * 
     * @param mxProductsThird 第三方商品
     * @return 结果
     */
    @Override
    public int insertMxProductsThird(MxProductsThird mxProductsThird)
    {
        return mxProductsThirdMapper.insertMxProductsThird(mxProductsThird);
    }

    /**
     * 修改第三方商品
     * 
     * @param mxProductsThird 第三方商品
     * @return 结果
     */
    @Override
    public int updateMxProductsThird(MxProductsThird mxProductsThird)
    {
        return mxProductsThirdMapper.updateMxProductsThird(mxProductsThird);
    }

    /**
     * 批量删除第三方商品
     * 
     * @param ids 需要删除的第三方商品主键
     * @return 结果
     */
    @Override
    public int deleteMxProductsThirdByIds(String[] ids)
    {
        return mxProductsThirdMapper.deleteMxProductsThirdByIds(ids);
    }

    /**
     * 删除第三方商品信息
     * 
     * @param id 第三方商品主键
     * @return 结果
     */
    @Override
    public int deleteMxProductsThirdById(String id)
    {
        return mxProductsThirdMapper.deleteMxProductsThirdById(id);
    }



    @Override
    public boolean pushDataByKFJ(List<MxProductsThirdVo> vo) {
        if (CollectionUtil.isEmpty(vo)) {
            throw new CustomException("请传入数据");
        }
        List<MxProductsThird> insertList = new ArrayList<>();
        for (MxProductsThirdVo thirdVo : vo) {
            MxProductsThird mxProductsThird = new MxProductsThird();
            BeanUtil.copyProperties(thirdVo, mxProductsThird);
            String id = IdUtil.getSnowflakeNextIdStr();
            mxProductsThird.setId(id);

            //设置商品id
            productDto product = thirdVo.getProduct();
            mxProductsThird.setProductId(product.getId());
            CommonDto user = thirdVo.getUser();
            if (ObjectUtil.isNotEmpty(product) ) {
                CommonDto category = product.getCategory();
                mxProductsThird.setCategoryId(category.getId());
                mxProductsThird.setCategoryName(category.getName());
                mxProductsThird.setProductName(product.getName());
                mxProductsThird.setProductType(product.getType());
                mxProductsThird.setSupplierId(user.getId());
                mxProductsThird.setSupplierName(user.getName());
            }
            insertList.add(mxProductsThird);
        }
        return this.saveBatch(insertList);
    }



}
